<template>
  <div>
    <i-dialog
      :title="dialogType === 'ADD' ? '添加角色' : '编辑角色'"
      :visible="visible"
      width="642px"
      :close-on-click-modal="false"
      @close="outerClose"
      @open="outerOpen"
    >
      <i-form
        :items="formItems"
        :model="form"
        :rules="rules"
        ref="form"
        label-width="95px"
        class="demo-form"
      >
      </i-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import { insertRole, updateRole } from '@/api/permission'
import { typeMap } from '@/utils/format'
import formValidate from '@/utils/validate'
export default {
  data() {
    return {
      form: {},
      rules: {
        showName: [
          {
            min: 1,
            max: 50,
            message: '请输入1-50位角色名称',
            trigger: 'blur',
          },
          {
            validator: formValidate('validateRoleShowname'),
            trigger: 'blur',
          },
        ],
        explanation: [
          {
            max: 200,
            message: '最多可输入200个字',
            trigger: 'blur',
          },
          {
            validator: formValidate('validateExplanation'),
            trigger: 'blur',
          },
        ],
      },
      formItems: [
        {
          type: 'text',
          value: 'showName',
          label: '角色名称：',
          placeholder: '请输入唯一的角色名称',
          clearable: true,
          required: true,
        },
        {
          type: 'select',
          value: 'userType',
          label: '用户类型：',
          placeholder: '请选择角色的用户类型',
          clearable: true,
          required: true,
          options: typeMap.selectOptions(['NORMAL_USER'], 'userType'),
          disabled: () => (this.dialogType === 'EDIT' ? true : false),
        },
        {
          type: 'textarea',
          value: 'explanation',
          autosize: { minRows: 4 },
          label: '备注：',
          placeholder: '请输入备注',
          clearable: true,
          required: true,
        },
      ],
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    dialogType: {
      type: String,
      default: 'ADD',
    },
    roleAttr: {
      type: Object,
      default: () => {},
    },
  },
  methods: {
    refreshTable() {
      this.$emit('refreshTable')
    },
    outerClose() {
      this.$refs['form'].resetFields()
      this.$emit('update:visible', false)
      this.$emit('update:roleAttr', {})
    },
    outerOpen() {
      if (this.dialogType === 'ADD') {
        this.form = {}
      } else if (this.dialogType === 'EDIT') {
        this.form = {
          showName: this.roleAttr.showName,
          userType: this.roleAttr.userType,
          explanation: this.roleAttr.explanation,
        }
      }
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        const handleMap = {
          ADD: {
            params: this.form,
            handler: insertRole,
            message: '新增成功',
          },
          EDIT: {
            params: { ...this.form, id: this.roleAttr.id },
            handler: updateRole,
            message: '编辑成功',
          },
        }
        handleMap[this.dialogType]
          .handler(handleMap[this.dialogType].params)
          .then(
            () => {
              this.$message({
                message: handleMap[this.dialogType].message,
                type: 'success',
              })
              this.refreshTable()
              this.outerClose()
            },
            () => {
              this.outerClose()
            }
          )
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.transfer-to-edit {
  position: relative;
  top: -403px;
  left: 183px;
}
</style>
